package entity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBUtil;
import util.Validator;

public class Park {

	public String id;
	public String type;
	public String note;
	
	public List<String[]> query(String[] params) {
		List<Object> list = new ArrayList<>();
		String sql = "select id, type, note from pm_park where true";
		if(Validator.isNotEmpty(params[0])){
			sql += " and type = ?";
			list.add(params[0]);
		}
		try {
			List<String[]> records = DBUtil.query(sql, list.toArray(), 3);
			return records;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return new ArrayList<String[]>();
	}
	
	public boolean save() {
		String sql = "insert into pm_park(id, type, note) values (?,?,?)";
		String[] paraArray = {id, type, note};
		try {
			int count = DBUtil.update(sql, paraArray);
			return count > 0;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}
	
	public List<String[]> queryIdle(String[] params) {
		List<Object> list = new ArrayList<>();
		String sql = "select id, type, note from pm_park where "
			+ "id not in (select parkId from pm_monthly_card where date_format(now(),'%Y-%m-%d') between startDate and endDate) "
				+"and id not in (select parkId from pm_pass_bill where entryIime is not null and leaveIime is null)";
		if(Validator.isNotEmpty(params[0])){
			sql += " and type = ?";
			list.add(params[0]);
		}
		try {
			List<String[]> records = DBUtil.query(sql, list.toArray(), 3);
			return records;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return new ArrayList<String[]>();
	}

	public boolean load() {
		String sql = "select type, note from pm_park where id = ?";
		Object[] paraArray = {id};
		try {
			String[] record = DBUtil.queryOne(sql, paraArray, 2);
			type = record[0];
			note = record[1];
			return true;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}

}
